/**
 * Created by chenz on 2019/4/16/0016.
 */
var zkTree = {
    zTree:null,
    rNode:'', //右键事件保存Node
    cNode:'', // 点击事件保存Node
    init:{
      initEvent:function(){
          zkTree.loadZKTreeNode();
          zkTree.bindItem();
      }
    },
    setting : {
        isSimpleData: true,
        //设置节点唯一标示名称
        treeNodeKey: "id",
        //设置节点的父节点名称
        treeNodeParentKey: "pid",
        //节点与节点之间的连线是否显示
        showLine: true,
        root:{
            isRoot:true,
            nodes:[]
        },
        callback:{
            click:function(event, treeId, treeNode){
            	zkTree.cNode=treeNode;
                zkTree.getNodeData(event,treeId,treeNode);
            },
            rightClick:function(event,treeId,treeNode){
            	zkTree.zTree.selectNode(treeNode);
                zkTree.rNode=treeNode;
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                if(treeNode.id=="/"){
                	zkTree.controllMenuItem({
                        y: event.clientY+scrollTop,
                        x: event.clientX,
                        addNode: true,
                        deleteNode: false,
                        expendNode: false
                    });
                }else{
                	zkTree.controllMenuItem({
                        y: event.clientY+scrollTop,
                        x: event.clientX,
                        addNode: true,
                        deleteNode: true,
                        expendNode: true
                    });
                }
            }
        }
    },
    loadZKTreeNode:function(){
        $.post("/zk/treeNode/v1/list",null,function(result){
            var zNodes=[];
            var rootNode = null;
            for(var i in result){
                var treeNode = {};
                treeNode['id']=result[i].nodePath;
                treeNode['pid']=result[i].parentNodePath;
                treeNode['name']=result[i].nodeName;
                treeNode['isParent']=result[i].parentNode;
                zNodes.push(treeNode);
                if(result[i].nodePath=="/"){
                	rootNode = treeNode;
                }
            }
            zkTree.zTree = $("#tree").zTree(zkTree.setting, zNodes);
            zkTree.zTree.expandNode(rootNode, true, false); 
        })
    },
    getNodeData:function(event, treeId, treeNode){
        var nodePath = treeNode.id;
        var parameter = {
            nodePath:nodePath
        };
        $.post("/zk/treeNode/v1/getTreeNode",parameter,function(result){
            var nodeValue = result.nodeValue;
            $("#nodeValue").val(nodeValue);
        })
    },
    controllMenuItem:function(rMenuJson){
        //显示div
        $("#rMenu").css({"top":(rMenuJson.y-5)+"px", "left":(rMenuJson.x-5)+"px", "display":"block"});
        if(rMenuJson.addNode){
            $("#addNode").show();
        }else{
            $("#addNode").hide();
        }
        if(rMenuJson.deleteNode){
            $("#deleteNode").show();
        }else{
            $("#deleteNode").hide();
        }
        if(rMenuJson.expendNode){
        	$("#expandNode").show();
        }else{
        	$("#expandNode").hide();
        }
    },
    bindItem:function(){
        //对菜单栏绑定hover事件
        $("#rMenu").hover(function(){
            //对菜单栏绑定click事件
            $("#deleteNode").unbind("click");
            $("#deleteNode").bind("click",function(){
                zkTree.deleteNode();
            });
            $("#addNode").unbind("click");
            $("#addNode").bind("click",function(){
                zkTree.addNode();
            });
            $("#expandNode").unbind();
            $("#expandNode").bind("click",function(){
            	zkTree.expandSelectNode(zkTree.rNode);
            });
        },function(){
            //划出之后，菜单栏隐藏
            $("#rMenu").hide();
        });
        $("#saveDataBtn").unbind();
        $("#saveDataBtn").bind("click",function(){
        	zkTree.saveData();
        });
    },
    addNode:function(){
    	var curNodePath = zkTree.rNode.id;
    	if(curNodePath=="/"){
    		curNodePath="";
    	}
        //prompt层
        layer.prompt({title: '输入节点名称', formType: 0}, function(newNodeName, index){
            var parameter = {
                curNodePath:curNodePath,
                newNodeName:newNodeName.trim()
            };
            $.post("/zk/treeNode/v1/addTreeNode",parameter,function(result){
                if(result=='true'){
                    layer.alert("新增节点：【"+newNodeName+"】成功");
                    var newNode = {
                        id:parameter.curNodePath+"/"+parameter.newNodeName,
                        pid:zkTree.rNode.id,
                        name:parameter.newNodeName,
                        isParent:true
                    };
                    zkTree.zTree.addNodes(zkTree.rNode,newNode);
                }else if(result=='false'){
                    layer.alert("新增节点【"+newNodeName+"】异常，请检查！");
                }else{
                	layer.alert(result);
                }
                layer.close(index);
            })
        });
    },
    deleteNode:function(){
    	var curNodePath = zkTree.rNode.id;
    	if(curNodePath=="/"){
    		layer.msg("无法支持删除根目录！");
    		return;
    	}
    	//询问框
    	layer.confirm("确认删除'"+curNodePath+"'节点及其节点下的所有节点！", {
    	  btn: ['确认','取消'] //按钮
    	}, function(){
    		var parameter = {
    				nodePath:curNodePath
    		}
    		//异步删除
    		$.post("/zk/treeNode/v1/deleteTreeNode",parameter,function(result){
    			if(result==true){
    				layer.msg('已删除该节点！');
    				zkTree.zTree.removeNode(zkTree.rNode);
    			}else{
    				layer.msg("出现异常，请检查！");
    			}
    		})
    	});
    },
    saveData:function(){
    	var nodeValue = $("#nodeValue").val().trim();
    	var parameter = {
    		nodePath:zkTree.cNode.id,
    		nodeValue:nodeValue	
    	};
    	$.post("/zk/treeNode/v1/updateTreeNodeData",parameter,function(result){
    		if(result==true){
    			layer.alert('保存节点数据成功', {
  				  icon: 1,
  				  skin: 'layer-ext-moon'
  				})
    			$("#nodeValue").val(nodeValue);
    		}else{
    			layer.alert('保存节点数据失败', {
				  icon: 2,
				  skin: 'layer-ext-moon'
				})
    		}
    	})
    },
    expandSelectNode:function(parentNode){
    	zkTree.zTree.expandNode(parentNode, true, false); 
    	var nodes = zkTree.zTree.getNodesByParam("pid",parentNode.id,parentNode); 
    	if(nodes.length>0){
    		$.each(nodes,function(index,node){
    			zkTree.expandSelectNode(node);
        	})
    	}
    }
};
$(function(){
    zkTree.init.initEvent();
});
